package com.young.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.young.common.interf.IBaseController;
import com.young.common.model.ResultData;
import com.young.common.utils.MD5Utils;
import com.young.manager.mapper.SysUserMapper;
import com.young.manager.model.SysUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("token")
public class TokenController implements IBaseController {

    private final SysUserMapper sysUserMapper;

    @GetMapping("user")
    ResultData<?> tokenUser(String account, String password, Integer type) {
        if (type == 1) {
            try {
                SysUser sysUser;
                QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
                userQueryWrapper.eq("account", account);
                sysUser = sysUserMapper.selectOne(userQueryWrapper, false);
                if (null == sysUser) {
                    userQueryWrapper.clear();
                    userQueryWrapper.eq("phone", account);
                    sysUser = sysUserMapper.selectOne(userQueryWrapper, false);
                }
                if (null != sysUser) {
                    String pwd = sysUser.getPassword();
                    String enPassword = MD5Utils.encrypt(password + sysUser.getSalt());
                    if (pwd.equals(enPassword)) {
                        return success(sysUser);
                    }
                }
            } catch (Exception e) {
                log.error("查询用户异常：", e);
            }
        }
        return success(null);
    }
}
